package com.hzqy.web.ac;


import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.hzqy.commons.utils.FileType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hzqy.commons.utils.ConstantUtils;
import com.hzqy.commons.utils.ConvertBeanUtil;
import com.hzqy.commons.utils.ExcelUtil;
import com.hzqy.service.wailian.WaiLianServiceBean;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.PmsUserVo;
import com.hzqy.web.vo.system.SysConfigVo;
import com.hzqy.web.vo.WlContentVo;

@Controller
@RequestMapping("/")
public class WaiLianAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	private static final String[] allowedFileType = new String[]{"png", "jpg", "gif","zip","jar","tar","docx","doc","xlsx","xls","flv","mp4","mp3","mpg","avi","mov","wmv"};
	@Resource
	private WaiLianServiceBean waiLianServiceBean;

	/**
	 * 添加外链
	 * 
	 */
	@RequestMapping("/wailian/insert_wailian")
	@ResponseBody 
	public Object insertWailian(WlContentVo wlvo ){
		Map<String, Object> resultMap= new HashMap<>();
		resultMap=waiLianServiceBean.insertWaiLian(wlvo);
		
		return resultMap;
	}
	/**
	 * 外链列表
	 * 
	 */
	@RequestMapping("/wailian/wailian_list")
	@ResponseBody 
	public Object wailianlist(HttpSession session,WlContentVo wlvo,int page, int rows ){
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		Map<String, Object> resultMap= new HashMap<>();
		resultMap=waiLianServiceBean.selectWaiLian(userVo,wlvo,page,rows);
		return resultMap;
	}
	
	@RequestMapping("/wailian/delete_wailian")
	@ResponseBody 
	public Object deleteWailian(int f_wlc_id ){
		Map<String, Object> resultMap= new HashMap<>();
		resultMap=waiLianServiceBean.deleteWailianContent(f_wlc_id);
		return resultMap;
	}
	
	@RequestMapping("/wailian/select_wailian")
	@ResponseBody 
	public WlContentVo selectWailian(int f_wlc_id ){
		WlContentVo contentVo = waiLianServiceBean.selectWaiLianById(f_wlc_id);
		return contentVo;
	}
	
	@RequestMapping("/wailian/update_wailian")
	@ResponseBody 
	public Object updateWailian(WlContentVo  wlContentVo){
		Map<String, Object> resultMap = new HashMap<>();
		resultMap = waiLianServiceBean.updateWailian(wlContentVo);
		return resultMap;
	}
	
	@RequestMapping("/wailian/review_wailian")
	@ResponseBody 
	public Object reviewWailian(HttpSession session,WlContentVo  wlContentVo){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		int flag = waiLianServiceBean.reviewWlContentVo(wlContentVo,userVo);
		if(flag > 0) {
			resultMap.put("success", true);
			resultMap.put("result","审核成功!");
		} else {
			resultMap.put("success", false);
			if(flag==-4){
				resultMap.put("result","审核失败,无该审核状态!flag="+flag);
			}else if(flag==-3){
				resultMap.put("result","审核失败,无审核权限!flag="+flag);
			}else if(flag==0){
				resultMap.put("result","审核失败!flag="+flag);
			}else{
				resultMap.put("result","审核失败!flag="+flag);
			}
		}
		return resultMap;
	}
/*
	@RequestMapping("/wailian/insert_batch_wailian")
	@ResponseBody 
	public Object insertBatchWailian(@RequestParam(value = "uploadify") MultipartFile file) throws Exception{
		if (FileType.checkFileType(file.getInputStream(), allowedFileType)) {
			StringBuffer str = new StringBuffer();
			StringBuffer str2 = new StringBuffer();
			int successCount = 0;
			InputStream is = null;
			try {
				LOG.debug("批量导入wailian开始");
				is = file.getInputStream();
				List<Map<String, Object>> list = ExcelUtil.parseExcel(is, ExcelUtil.ALLOWED_EMPTY_MODE, new String[]{});
				LOG.debug("解析excel完成, 开始循环入库");
				for (int i = 0; i < list.size(); i++) {
					try {
						boolean success = false;
						Map<String, Object> m = list.get(i);
						WlContentVo wlContentVo = ConvertBeanUtil.getBeanFromMap(m, WlContentVo.class, WlContentVo.class.getName());
						wlContentVo.setF_wlc_status(1);
						Map<String, Object> resultMap = waiLianServiceBean.insertWaiLian(wlContentVo);
						success = (Boolean) resultMap.get("success");
						successCount += (success ? 1 : 0);
						if (!success) {
							str2.append(i + 1).append(",");
						}
					} catch (Exception e) {
						LOG.error("", e);
						//str.append("第").append(i+1).append("条 出错! ").append(e.getMessage()).append("<br/>");
					}
				}
				LOG.debug("批量完成");
				if (str2.length() > 0) {
					str.append("第").append(str2.substring(0, str2.length() - 1)).append("条导入失败! ").append("<br/>");
				}
				str.append("批量导入完成， 一共：").append(list.size()).append("条数据, 成功").append(successCount).append(", 失败").append(list.size() - successCount);
			} catch (Exception e) {
				LOG.error("", e);
				//str.append("批量导入失败: " + e.getMessage());
			} finally {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
					}
				}
			}
			return str.toString();
		} else {
			return "";
		}
	}*/
	
	@RequestMapping("/wailian/selectSysConfig")
	@ResponseBody 
	public SysConfigVo selectSysConfig(SysConfigVo sysConfigVo) {
		SysConfigVo sc = waiLianServiceBean.selectSysConfig(sysConfigVo);
		return sc;
	} 
	
	//查询外链
	@RequestMapping("/wailian/selectWLContent")
	@ResponseBody 
	public WlContentVo selectWLContent(WlContentVo wlContentVo) {
		WlContentVo wlc = waiLianServiceBean.selectWLContent(wlContentVo);
		return wlc;
	} 
	
	//查询有效的外链
	@RequestMapping("/wailian/selectWLContentAvaliable")
	@ResponseBody
	public WlContentVo selectWLContentAvaliable(WlContentVo wlContentVo) {
		WlContentVo wlc = waiLianServiceBean.selectWLContentAvaliable(wlContentVo);
		return wlc;
	} 
	
	/**
	 * 批量下线
	 * @param ids
	 * @return
	 */
	@RequestMapping("/wailian/batch_unreview_wailian")
	@ResponseBody
	public Object batchUnreviewWailian(String ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = waiLianServiceBean.batchUnReviewContent(ids);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量下线成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","批量下线失败!flag="+flag);
		}
		return resultMap;
	}
	
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/wailian/batch_delete_wailian")
	@ResponseBody
	public Object batchDeleteContent(String ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = waiLianServiceBean.batchDeleteContent(ids);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量删除成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","批量删除失败!flag="+flag);
		}
		return resultMap;
	}
}
